package com.hanliy.storage.config;

import com.hanliy.storage.BaseStorageService;
import com.hanliy.storage.impl.AliyunStorageImpl;
import com.hanliy.storage.impl.LocalStorageImpl;
import com.hanliy.storage.impl.QiniuStorageImpl;
import com.hanliy.storage.impl.TencentStorageImpl;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * @author hanyulin
 * @apiNote
 * @date 2021/12/3 15:42
 */
@Configuration
@EnableConfigurationProperties(StorageProperties.class)
public class StorageAutoConfiguration {

    private final StorageProperties properties;

    public StorageAutoConfiguration(StorageProperties properties) {
        this.properties = properties;
    }

    /**
     * 获取配置参数里面的active值，通过active的不同，基础的存储服务类的中的存储模式也不同
     *
     * @author hanyulin
     * @date 2021/12/4 10:57
     * @return BaseStorageService
     */
    @Bean
    public BaseStorageService baseStorageService() {
        BaseStorageService baseStorageService = new BaseStorageService();
        String active = this.properties.getActive();
        baseStorageService.setActive(active);
        if ("local".equals(active)) {
            baseStorageService.setStorageType(localStorage());
        } else if ("aliyun".equals(active)) {
            baseStorageService.setStorageType(aliyunStorage());
        } else if ("tencent".equals(active)) {
            baseStorageService.setStorageType(tencentStorage());
        } else if (active.equals("qiniu")) {
            baseStorageService.setStorageType(qiniuStorage());
        } else {
            throw new RuntimeException("当前存储模式 " + active + " 不支持");
        }
        return baseStorageService;
    }

    /**
     * 存储模式为local，注入LocalStorageImpl需要的属性
     *
     * @author hanyulin
     * @date 2021/12/4 11:11
     * @return com.hanliy.storage.impl.LocalStorageImpl
     */
    @Bean
    public LocalStorageImpl localStorage() {
        LocalStorageImpl localStorage = new LocalStorageImpl();
        StorageProperties.Local local = this.properties.getLocal();
        localStorage.setAddress(local.getAddress());
        localStorage.setStoragePath(local.getStoragePath());
        return localStorage;
    }

    @Bean
    public AliyunStorageImpl aliyunStorage(){
        StorageProperties.Aliyun aliyun = this.properties.getAliyun();
        AliyunStorageImpl aliyunStorage = new AliyunStorageImpl();
        aliyunStorage.setEndpoint(aliyun.getEndpoint());
        aliyunStorage.setAccessKeyId(aliyun.getAccessKeyId());
        aliyunStorage.setAccessKeySecret(aliyun.getAccessKeySecret());
        aliyunStorage.setBucketName(aliyun.getBucketName());
        return aliyunStorage;
    }

    @Bean
    public TencentStorageImpl tencentStorage(){
        StorageProperties.Tencent tencent = this.properties.getTencent();
        TencentStorageImpl tencentStorage = new TencentStorageImpl();
        tencentStorage.setSecretId(tencent.getSecretId());
        tencentStorage.setSecretKey(tencent.getSecretKey());
        tencentStorage.setRegion(tencent.getRegion());
        tencentStorage.setBucketName(tencent.getBucketName());
        return tencentStorage;
    }

    @Bean
    public QiniuStorageImpl qiniuStorage(){
        StorageProperties.Qiniu qiniu = this.properties.getQiniu();
        QiniuStorageImpl qiniuStorage = new QiniuStorageImpl();
        qiniuStorage.setEndpoint(qiniu.getEndpoint());
        qiniuStorage.setAccessKey(qiniu.getAccessKey());
        qiniuStorage.setSecretKey(qiniu.getSecretKey());
        qiniuStorage.setBucketName(qiniu.getBucketName());
        return qiniuStorage;
    }


}
